package com.julis.activity.obu_function;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Constant;
import com.baseutils.File_Z;
import com.baseutils.SpfUtils;
import com.bean.TradeMessage;
import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.db.Z_ObuInfoDao;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.thread.FalsePurchaseThread2;
import com.thread.OffLineThread;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * obu零元消费
 */
public class OBU_Trade2_Activity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.bt_Shangchan)
    RoundButton bt_Shangchan;
    @BindView(R.id.bt_Start)
    RoundButton btStart;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.tishi)
    TextView tishi;
    @BindView(R.id.tongji)
    TextView tongji;
    @BindView(R.id.tv_obu)
    TextView tv_time;
    static String path, time2;
    boolean psamopen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tradeinfo2);
        ButterKnife.bind(this);
        z_obuInfoDao = new Z_ObuInfoDao(this);
        psamopen = (boolean) SpfUtils.get(this, Constant.Check_Psam, true);
        OffLineThread setParam = new OffLineThread("A9_2", OBU_Trade2_Activity.this);
        setParam.start();
        path = File_Z.createFile(OBU_Trade2_Activity.this);


    }

    String message;
    String type = "";
    String tishimessage, tishimessageVALUE;
    FalsePurchaseThread2 purchase;
    TradeMessage tradeMessage;
    String mac = "";
    @SuppressLint("HandlerLeak")
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tradeMessage = (TradeMessage) msg.obj;
            mac = tradeMessage.getMac();
            String Message = tradeMessage.getMessage();
            if (dialog1 != null) {
                dialog1.dismiss();
            }

            switch (msg.what) {
                case 1:
                    type = "成功";
                    tishimessage = "预设" + tradeMessage.getnTradeTotal() + "次，" + ",当前第" + tradeMessage.getnTradeCount() + "次交易" + " " + type + "，总计交易" + tradeMessage.getnTradeCount() + "次，当前交易成功" + tradeMessage.getnTradeSuccessNumber() + "次，失败" + tradeMessage.getnTradeFailNumber() + "次" + "当前mac:" + mac;
                    tishimessageVALUE = "预设" + tradeMessage.getnTradeTotal() + "次" + "\n" + "当前第" + tradeMessage.getnTradeCount() + "次交易" + "\n" + Message + "\n" + "总计交易" + tradeMessage.getnTradeCount() + "次" + "\n" + "当前交易成功" + tradeMessage.getnTradeSuccessNumber() + "次，" + "失败" + tradeMessage.getnTradeFailNumber() + "次" + "\n" + "当前mac:" + mac + "\n";
                    tishi.setText("当前MAC:" + mac);

                    break;
                case 2:
                    type = "失败";
                    tishimessage = "预设" + tradeMessage.getnTradeTotal() + "次，" + ",当前第" + tradeMessage.getnTradeCount() + "次交易" + " " + type + "，总计交易" + tradeMessage.getnTradeCount() + "次，当前交易成功" + tradeMessage.getnTradeSuccessNumber() + "次，失败" + tradeMessage.getnTradeFailNumber() + "次" + "当前mac:" + mac;
                    tishimessageVALUE = "预设" + tradeMessage.getnTradeTotal() + "次" + "\n" + "当前第" + tradeMessage.getnTradeCount() + "次交易" + "\n" + Message + "\n" + "总计交易" + tradeMessage.getnTradeCount() + "次" + "\n" + "当前交易成功" + tradeMessage.getnTradeSuccessNumber() + "次，" + "失败" + tradeMessage.getnTradeFailNumber() + "次" + "\n" + "当前mac:" + mac + "\n";

                    break;

                default:
                    return;
            }
            message = tradeMessage.getMessage();
            bt_Shangchan.setVisibility(View.VISIBLE);
            tongji.setText("提示：" + message);
            if (message.equals("零元消费成功")) {
                tongji.setTextColor(getResources().getColor(R.color.ym_green));
            } else {
                tongji.setTextColor(getResources().getColor(R.color.red));
            }

            tv_time.setText("本次交易时长：" + tradeMessage.getTime());
            tingzhi = false;


        }
    };
    Z_ObuInfoDao z_obuInfoDao;
    int tradeTotal = 1;
    boolean tingzhi = false;

    @OnClick({R.id.img_back, R.id.bt_Start, R.id.bt_Shangchan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                tingzhi = true;

                finish();
                break;
            case R.id.bt_Shangchan:

                finish();
                break;

            case R.id.bt_Start:

                time2 = File_Z.GetNowDate2();
                tv_time.setText("提示：");
                startobu();


                break;


        }
    }

    public void startobu() {
        tishi.setText("当前MAC:");
        tongji.setText("交易测试");
        tradeTotal = 1;
        start();
    }


    KProgressHUD dialog1;

    public void showdialog1() {

        String data="正在测试"+Constant.ManufacturerName+"中";
        dialog1 = KProgressHUD.create(OBU_Trade2_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel(data)
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void start() {
        showdialog1();
        int tradetime = (int) SpfUtils.get(OBU_Trade2_Activity.this, "tradetime", 2);
        purchase = new FalsePurchaseThread2(handler1, OBU_Trade2_Activity.this, psamopen, tradeTotal, tradetime * 1000);
        purchase.start();
    }

    public void stop() {
        purchase.setRun(false);
    }
}
